package ai.tasks;

import ai.AIConstant;
import ai.Trackable;
import ai.PathFinding;
import ai.AIUtil;
import ai.engine.AIResource;
import ai.engine.AIUnit;
import ai.engine.Task;
import ai.engine.TaskDoer;
import ai.engine.factories.TaskFactory;
import engine.UnitAction;
import engine.data.UnitType;
import util.ArrayList;
import util.List;

public class BuildCityTask extends Task {

	private boolean isObjectiveAchieved;

	public BuildCityTask(short id, Trackable target) {
		super(id, TaskFactory.BUILD_CITY_TASK, AIUnit.class, "Build City", "",
				AIConstant.PRIORITY_STRONG, target, (byte) 1, (byte) 1);
		isObjectiveAchieved = false;
	}

	public Trackable getTarget() {
		return (Trackable) target;
	}

	public boolean isTaskSuitable(TaskDoer possibleTaskDoer) {
		AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return possibleUnitDoer.type == UnitType.SETTLER;
	}

	public short getFormula(TaskDoer possibleTaskDoer) {
		//AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return (short) (this.priority);
	}

	public byte getAction(TaskDoer taskDoer) {
		AIUnit unitDoer = (AIUnit) taskDoer;
		if (!this.assignedResources.contains(unitDoer))
			return UnitAction.SKIP;
		if (!AIUtil.isOnSameTile(unitDoer, getTarget())) {
			// Go to the tile where the city will be built
			return PathFinding.getNextDirection(PathFinding.TYPE_QUICKEST,
					unitDoer, getTarget());
		} else {
			// Build the city
			isObjectiveAchieved = true;
			return UnitAction.BUILD;
		}
	}

	public List getAdditionalResourcesNeeded() {
		List resourcesNeeded = new ArrayList();
		if (getAssignedResources().size()<resourcesMin) resourcesNeeded.add(new AIResource(getTarget(), UnitType.SETTLER));
		return resourcesNeeded;
	}
	
	public boolean isObjectiveAchieved() {
		return isObjectiveAchieved;
	}

}
